package models

import (
	"fmt"
	"github.com/astaxie/beego/orm"
	"strconv"
)

type AlarmrecordInfo struct {
	Id          int64
	Deviceid    string
	Devicetype  string
	Alarmtype   string
	Company     string
	Alarmstamp  string
	Alarmtime   string
}

//查询所有告警信息
func ReadAlarm(alarmDataChan chan string,company string,page string, size string,querytype string)int{
    //fmt.Println(page)
    //fmt.Println(size)
	//使用原生查询
	var alarmrows []AlarmrecordInfo
	o := orm.NewOrm()
	pageint ,_ := strconv.ParseInt(page, 10, 64)
	sizeint, _ := strconv.ParseInt(size, 10, 64)
	var totalItem int = 0
	//1为查询人员告警，2为查询设备告警
	if querytype == "1" {
    o.Raw("SELECT COUNT(*) FROM alarmrecord_info where company = ? and alarmtype IN ('佩戴告警','心率告警','近电告警','紧急呼救')",company).QueryRow(&totalItem)
	//fmt.Println(totalItem)
	if _, err := o.Raw("SELECT * FROM alarmrecord_info where company = ? and alarmtype IN ('佩戴告警','心率告警','近电告警','紧急呼救')  ORDER BY alarmtime DESC limit ?,?",company,(pageint-1)*sizeint, sizeint).QueryRows(&alarmrows); err != nil{
		fmt.Println("err")
		return 0
	}}else if querytype == "2" {
         o.Raw("SELECT COUNT(*) FROM alarmrecord_info where company = ? and alarmtype = '低电量告警'",company).QueryRow(&totalItem)
         //fmt.Println(totalItem)
         if _, err := o.Raw("SELECT * FROM alarmrecord_info where company = ? and alarmtype = '低电量告警'  ORDER BY alarmtime DESC limit ?,?",company,(pageint-1)*sizeint, sizeint).QueryRows(&alarmrows); err != nil{
          fmt.Println("err")
         return 0
       }
       }
	//fmt.Println("n =",n)
	var  i int64= (pageint-1)*sizeint;
	for _,alarmrow := range alarmrows{
		i++
		id := strconv.FormatInt(i,10)
		alarmInfos :=id+ "and"+alarmrow.Deviceid+
			"and"+alarmrow.Devicetype+
			"and"+alarmrow.Alarmtype+
			"and"+alarmrow.Company+
			"and"+alarmrow.Alarmtime
		alarmDataChan <- alarmInfos
	}
	return totalItem
}

//条件查询人员告警记录
func Queryalarmbycondition(alarmDataChan chan string,company string,starttime string, endtime string,alarmtype string, deviceid string,page string, size string) int {
	var alarmrows []AlarmrecordInfo
	o := orm.NewOrm()
	pageint ,_ := strconv.ParseInt(page, 10, 64)
	sizeint, _ := strconv.ParseInt(size, 10, 64)
	var totalItem int = 0

	if deviceid == "notdeviceid" {  //根据时间段和告警类型查询
		o.Raw("SELECT COUNT(*) FROM alarmrecord_info WHERE company = ? AND alarmtype = ? AND alarmtime BETWEEN ? AND ?", company, alarmtype, starttime, endtime).QueryRow(&totalItem)
		//fmt.Println(totalItem)
		if _, err := o.Raw("SELECT * FROM alarmrecord_info WHERE company = ? AND alarmtype = ? AND alarmtime BETWEEN ? AND ? ORDER BY alarmtime DESC limit ?,? ", company, alarmtype, starttime, endtime, (pageint-1)*sizeint, sizeint).QueryRows(&alarmrows); err != nil {
			fmt.Println("err")
			return 0
		}
	}else{ //根据设备id获取告警记录
	    if alarmtype == "1" { //人员告警
			o.Raw("SELECT COUNT(*) FROM alarmrecord_info WHERE company = ? AND alarmtype IN ('佩戴告警','心率告警','近电告警','紧急呼救') AND deviceid = ?", company,  deviceid).QueryRow(&totalItem)
			//fmt.Println(totalItem)
			if _, err := o.Raw("SELECT * FROM alarmrecord_info WHERE company = ? AND alarmtype IN ('佩戴告警','心率告警','近电告警','紧急呼救') AND deviceid = ? ", company,  deviceid).QueryRows(&alarmrows); err != nil {
				fmt.Println("err")
				return 0
			}
		} else if alarmtype == "2" { //设备告警
			o.Raw("SELECT COUNT(*) FROM alarmrecord_info WHERE company = ? AND alarmtype = '低电量告警' AND deviceid = ?", company, deviceid).QueryRow(&totalItem)
			//fmt.Println(totalItem)
			if _, err := o.Raw("SELECT * FROM alarmrecord_info WHERE company = ? AND alarmtype = '低电量告警' AND deviceid = ? ", company, deviceid).QueryRows(&alarmrows); err != nil {
				fmt.Println("err")
				return 0
			}
		}
	}
	//fmt.Println("n =",n)
	var  i int64= (pageint-1)*sizeint;
	for _,alarmrow := range alarmrows{
		i++
		id := strconv.FormatInt(i,10)
		alarmInfos :=id+ "and"+alarmrow.Deviceid+
			"and"+alarmrow.Devicetype+
			"and"+alarmrow.Alarmtype+
			"and"+alarmrow.Company+
			"and"+alarmrow.Alarmtime
		alarmDataChan <- alarmInfos
	}
	return totalItem
}

//搜索告警记录
func SearchAlarm(AlarmDataChan chan string,username string,querylittletype string) int {
	//fmt.Println("get messge from front:",message);
	company := QueryCompanyByUsername(username)
	fmt.Println(company)
	var alarmrows []AlarmrecordInfo
	var totalItem int = 0
	o := orm.NewOrm()
	if querylittletype == "1" {
		o.Raw("SELECT COUNT(*) FROM alarmrecord_info where company = ? and alarmtype IN ('佩戴告警','心率告警','近电告警','紧急呼救')",company).QueryRow(&totalItem)
		fmt.Println(totalItem)
		if _, err := o.Raw("SELECT * FROM alarmrecord_info WHERE company = ? AND alarmtype IN ('佩戴告警','心率告警','近电告警','紧急呼救')", company).QueryRows(&alarmrows); err != nil {
			fmt.Println("err")
			return 0
		}
	}else if querylittletype == "2" {
		o.Raw("SELECT COUNT(*) FROM alarmrecord_info where company = ? and alarmtype = '低电量告警'",company).QueryRow(&totalItem)
		if _, err := o.Raw("SELECT * FROM alarmrecord_info  where company = ? and alarmtype  = '低电量告警'", company).QueryRows(&alarmrows); err != nil {
			fmt.Println("err")
			return 0
		}
	}

	//fmt.Println("n =",n)
	var  i int64= 1;
	for _,alarmrow := range alarmrows{
		i++
		id := strconv.FormatInt(i,10)
		alarmInfos :=id+ "and"+alarmrow.Deviceid+
			"and"+alarmrow.Devicetype+
			"and"+alarmrow.Alarmtype+
			"and"+alarmrow.Company+
			"and"+alarmrow.Alarmtime
		AlarmDataChan <- alarmInfos
		i++
	}
	return totalItem
}














